/**
 * @author: welling
 */
Weifox.ui={

	/**
	 * 创建一个链接节点，并为节点加上适当的属性值，给MsgContainer判断如何打开。
	 * @param {Object} url
	 * @param {Object} text
	 */
    createLink: function(url, text,wtype){
		try {
			var anchor = document.createElement("a");
			if (!wtype)
				wtype='link'; 
			
			anchor.setAttribute('wtype', wtype);//链接的节点类型

			anchor.setAttribute('url', url);//链接的原始地址
			anchor.setAttribute('text', text);//显示在链接上的字符
			if (text.length > 19) {
				text = text.substring(0, 19) + '...';
			}
			anchor.appendChild(document.createTextNode(text));
			
		}catch(e){
			alert(e)
		}
	    return anchor;
    },
	
	onContextMenu:function(event,t){
		var box=event.target.boxObject;
		var edit=document.getElementById('weifox-edit-panel');
		edit.hidePopup();
		edit.open({x:box.screenX-200,y:box.screenY-140});
		switch(t){
			case 'pic':
			case 'bg':
				var m = gContextMenu;
				if(!m)return;
		        if (m.onImage || m.hasBGImage) {
					var url="";
					if (m.onImage) {
						url = m.mediaURL;
					}
					else {
						url = m.bgImageURL;
					}
					if (url != "") {
						edit.addPicURL(url);
					}
				}
				break;
			case 'link':
				var b=Weifox.browser;
				edit.insertString(decodeURIComponent(escape('我正在看 '))+b.getCurrentTitle()+' '+b.getCurrentLink()+' ');
				break;
		}
	},
	/**
	 * 传入一个图片地址,打开Weifox的图片浏览器
	 * @param {Object} url
	 */
	openImagePanel:function(url){
		var ip=this.imagePanel;
		var style=Weifox.browser.pref.getCharPref('image_panel_pos');
		var panel=document.getElementById('weifox-panel');
		ip.open(panel,style,url);
	},
	/**
	 * 
	 * @param {Object} evt
	 */
	toggleEditMessage:function(e,data){
		var panel = document.getElementById('weifox-edit-panel');
		var node;
		if(e)
			node=e.target;
		else
			node=document.getElementById('weifox-bottom-send');
		
		/*
		if (panel.state != 'closed') {
			panel.hidePopup();
			return ;
		}*/
		panel.hidePopup();
		if(data){//如果不带参，就是发微博，如果带参就是发评论私信等
			panel.open({
				node: node,
				desc: 'start_after',
				x: -10
			},data);
		}else{
			panel.open({
				node:node,
				desc:'start_after',
				x:-10
			});
		}
	},
	toggleUsermanagerPanel:function(){
		var d=document;
		var m = d.getElementById('weifox-panel');
		var b = d.getElementById('weifox-toolbarbutton');
		
		var panel = d.getElementById('weifox-usermanager-panel');
		if (panel.state == 'open') {
			panel.hidePopup();
			return;
		}
		if (m.state == 'closed') {
			panel.openPopup(b, 'after_end');
		}
		else {
			panel.openPopup(m, 'start_before');
		}
	},
	showSettingMenu:function(evt){
		var node=evt.target;
		var panel=document.getElementById('weifox-setting-menu');
		panel.openPopup(node,'after_end');
	},
	
	toggleMainPanel:function(evt){
		var panel=document.getElementById('weifox-panel');
		if (panel.state != 'closed') {
			panel.hidePopup();
			return ;
		}
		
		var x=Weifox.browser.pref.getIntPref('pos_x');
		var y=Weifox.browser.pref.getIntPref('pos_y');
		if(x==-1||y==-1){//
			panel.openPopup(evt.target,'after_end');
		}else{
			panel.openPopupAtScreen(x,y);
		}
	},
	toggleUserList:function(){
		var d=document;
        var m=d.getElementById('weifox-panel');
        var b=d.getElementById('weifox-toolbarbutton');
        
        var panel=d.getElementById('weifox-userlist-panel');
        if (panel.state == 'open') {
                panel.hidePopup();
                return;
        }
        if(m.state=='closed'){
                panel.openPopup(b,'after_end');
        }else{
                panel.openPopup(m,'start_before');
        }

	},
	init:function(){
		var doc=document;
		
		this.userlist=doc.getElementById('weifox-user-list');

		this.weifoxMain=doc.getElementById('weifox-main');
		this.weifoxBody=doc.getElementById('weifox-body');
		this.imagePanel=doc.getElementById('weifox-image-panel');
		
		//初始化
		
		//设置窗口大小
		var pref=Weifox.browser.pref;
		var w=pref.getIntPref('width');
		this.weifoxBody.setAttribute('width',w);
		this.weifoxBody.setAttribute('height',pref.getIntPref('height'));
		
		
		//设置拖动事件
		this.installMoveEvent();
		this.installResizeEvent();
		
		var tw=pref.getIntPref('treewidth');
		
		var tr=doc.getElementById("weifox-tree-view");
		if(tw==0){
			doc.getElementById("weifox-tree-view-splitter").setAttribute('state',"collapsed");
		}else 
			tr.setAttribute('width',tw);
		
		this.adjustWidth(w-tw-10);
	},
	resetPosition:function(){
		var panel=document.getElementById('weifox-panel');
		Weifox.browser.pref.setIntPref('pos_x',-1);
		Weifox.browser.pref.setIntPref('pos_y',-1);
		panel.hidePopup();
	},
	installMoveEvent:function(){
		var doc=document;
		var panel=doc.getElementById('weifox-panel');
		var title=this.weifoxBody.childNodes[0];//第一个节点就是标题
		var drag=false;
		var ox,oy;
		var x,y;
		title.addEventListener('mousedown',function(event){
			if(event.target!=event.currentTarget){//如果按到的不是标题，而是标题上面的控件，则退出
				return;
			}
			x=panel.boxObject.screenX;
			y=panel.boxObject.screenY;			
			ox=event.clientX;
			oy=event.clientY;
			drag=true;
		},false);
		doc.addEventListener('mousemove',function(event){
			if (drag) {
				var dx = event.clientX - ox;
				var dy = event.clientY - oy;
				panel.moveTo(x + dx, y + dy);
			}
		},false);
		doc.addEventListener('mouseup',function(event){
			if (drag) {
				drag = false;
				Weifox.browser.pref.setIntPref('pos_x',panel.boxObject.screenX);
				Weifox.browser.pref.setIntPref('pos_y',panel.boxObject.screenY);
				
				//hack:用来使发送框跟随到主窗口
				var b=Weifox.ui.weifoxBody;
				var w=b.boxObject.width;
				b.setAttribute('width',w+1);
				b.setAttribute('width',w);
			}
		},false);
		
	},
	onDragSplitter:function(){
		this.adjustWidth();
		//保存存
		var pref=Weifox.browser.pref;
		var tr=document.getElementById("weifox-tree-view");
		pref.setIntPref('treewidth',tr.clientWidth);
	},
	/**
	 * 将所有的节点宽度回定。
	 */
	adjustWidth:function(width){
		var w;
		var d=document;
		if(!width){
			w=d.getElementById("weifox-main").clientWidth;
		}else{
			w=width;
		}
		//----
		var selector='#weifox-panels .weifox-msg';
		if (!this._____styleSheet) 
			for (var i = 0; i < d.styleSheets.length; i++) {
				if (d.styleSheets[i].href.toLowerCase() == 'chrome://weifox/content/ui/main.css') {
					this._____styleSheet = d.styleSheets[i];
					break;
				}
			}
		//get style by selector
		if(!this.____style)
		for (var i = 0; i < this._____styleSheet.cssRules.length; i++) {
			if (this._____styleSheet.cssRules[i].selectorText == selector) {
				this.____style = this._____styleSheet.cssRules[i].style;
				break;
			}
		}
		
		if(this.____style){
			this.____style.width=w-18+'px';
		}
	},
	minWidth:430,
	minHeight:450,
	installResizeEvent:function(){
		var doc=document;
		var resizer=doc.getElementById('weifox-panel-resizer');
		var body=this.weifoxBody;
		var drag=false;
		var ox,oy;
		var width,height;
		resizer.addEventListener('mousedown',function(event){
			ox=event.clientX;
			oy=event.clientY;
			width=body.boxObject.width;
			height=body.boxObject.height;
			drag=true;
		},false);
		doc.addEventListener('mousemove',function(event){
			if (drag) {
				var w = event.clientX - ox+width;
				var h = event.clientY - oy+height;
				if(w<430)w=430;
				if(h<450)h=450;
				body.setAttribute('width',w);
				body.setAttribute('height',h);
			}
		},false);
		doc.addEventListener('mouseup',function(event){
			if (drag) {
				drag = false;
				Weifox.browser.pref.setIntPref('width',body.boxObject.width);
				Weifox.browser.pref.setIntPref('height',body.boxObject.height);
				Weifox.ui.adjustWidth();
			}
		},false);		
	},
	weifoxMain:null,
	imagePanel:null,
	/**
     * 
     * @param {Object} msg 服务器返回的JSON对象
     * @param {Object} tpl 模板名称,定义在content/ui/template.css
     * 
     * @return 一个XUL节点
     */
    createNodeByTemplate: function(tpl, msg){
        var newNode = document.createElement('box');
        newNode.className = tpl;
        newNode.setContent(msg);
        return newNode;
    },
	addUser:function(aUser){
		document.getElementById("weifox-tree-view").addUser(aUser);
		var id=aUser.app+aUser.user_id;
		var cls='tpl-'+aUser.app+'-';
		var tls=aUser.timeline;
		var tl=null;
		for (var tln in tls) {//TODO:
			tl=tls[tln];
			//是否加入
			if(tl.disabled!=true)
				this.weifoxMain.addTab(id+tln,cls+tln,aUser);
		}
		
		//把User加入EditPanel
		document.getElementById('weifox-edit-panel').addUser(aUser);
		
		//加入用户管理
		this.userlist=document.getElementById('weifox-user-list');
		
		var elem=document.createElement('box');
        elem.className='weifox-usericon';
        
        elem.setAttribute('app','chrome://weifox/content/ui/res/'+aUser.app+'.ico');
        elem.setAttribute('user_id',aUser.user_id);
        elem.setAttribute('user_name',aUser.user_name);
        elem.setAttribute('user_icon',aUser.user_icon);
        elem.setAttribute('method',aUser.method);
        this.userlist.appendChild(elem);
		elem.user=aUser;

	},
	/**
	 * 工具栏上面的提示数.
	 */
	updateUnread:function(){
		
	}
};

